package com.github.tetrisgame.ui.view

import android.R.attr.angle
import android.animation.Animator
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.text.TextPaint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.github.tetrisgame.utils.UIUtils
import com.github.tetrisgame.utils.UIUtilskt
import okhttp3.Callback

//主构造函数
/**
 * 1. 测量尺寸
 * 2. 将canvas裁剪为圆形
 * 3. 设置背景颜色
 * 4. 绘制跳转文本
 * 5. 绘制圆弧
 * 6. 启动圆弧动画
 */
class JumpProgressView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    //记录默认尺寸
    //int->Int float->Float long->Long
    var mSize: Int = UIUtilskt.dp2px(100)

    //记录裁剪的圆形路径 懒加载 单例设计模式
    //懒加载只会加载一次
    val mCircleClipPath: Path by lazy {
        Path().apply {
            reset()
            moveTo(0f, 0f)
            lineTo(0f, 0f)
            addCircle(width / 2f, height / 2f, mSize / 2f, Path.Direction.CW)
        }
    }

    val mTextPaint: TextPaint = TextPaint(Paint.LINEAR_TEXT_FLAG).apply {
        textSize = UIUtilskt.sp2px(20)
    }

    val mArcPaint: Paint by lazy {
        Paint().apply {
            style = Paint.Style.STROKE
            strokeWidth = UIUtilskt.dp2px(6).toFloat()
        }
    }

    /*
     1. 生命一个变量保存数据 field
     2. 生成setter方法 setAngle()
     3. 生成getter方法  getAngle()
     */
    var angle: Float = 0f
        set(value) {
            field = value
            invalidate()
        }

    var age: Int = 10

    private var onClick: (View) -> Unit = {}

    //高阶函数
    fun addOnClickCallback(callback: (View) -> Unit) {
        onClick = callback
    }

    //普通函数
    fun start(onEnd: () -> Unit = {}) {
        ObjectAnimator.ofFloat(this, "angle", 0f, 360f).apply {
            duration = 3000
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationStart(animation: Animator) {

                }

                override fun onAnimationEnd(animation: Animator) {
                    onEnd()
                }

                override fun onAnimationCancel(animation: Animator) {

                }

                override fun onAnimationRepeat(animation: Animator) {

                }
            })
            start()
        }
    }

    /**
     * kotlin中提供了很多的语法糖
     * 让代码更简洁
     */
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mSize = width

    }

    override fun onDraw(canvas: Canvas) {
        canvas.apply {
            clipPath(mCircleClipPath)
            drawColor(Color.MAGENTA)
            mTextPaint.measureText("跳转").also { textWidth ->
                mTextPaint.fontMetrics.also {
                    val dy = (it.bottom - it.top) / 2 - it.bottom
                    drawText(
                        "跳转",
                        (width - textWidth) / 2f,
                        height / 2 + dy,
                        mTextPaint
                    )
                }
            }

            drawArc(
                UIUtils.dp2pxF(3),
                UIUtils.dp2pxF(3),
                width - UIUtils.dp2pxF(3),
                height - UIUtils.dp2pxF(3),
                -90f,
                angle,
                false,
                mArcPaint
            )
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN) {
            onClick(this)
            return true
        }
        return super.onTouchEvent(event)
    }

}